package com.just521.xinshoutong.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.just521.xinshoutong.R;

public class ToastFuncList extends LinearLayout {
	ListView mlistview;
	Animation animationin;
	Animation animationout;
	String mpos;
	public ToastFuncList(Context context) {
		super(context);
		init(context,null);
	}
	
	public ToastFuncList(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}
	private void init(Context context, AttributeSet attrs){
		if(attrs!=null){
			TypedArray a = context.obtainStyledAttributes(attrs,  
	                R.styleable.view_ToastFuncList); 
			mpos=a.getString(R.styleable.view_ToastFuncList_disp_pos);
			if(mpos.equals("middle")){
				
				LayoutInflater inf= LayoutInflater.from(context);
				View layout= inf.inflate(R.layout.toast_func, null);
				mlistview=(ListView)layout.findViewById(R.id.toast_func_list);
				this.addView(layout);
			}else if(mpos.equals("right")){
				
				LayoutInflater inf= LayoutInflater.from(context);
				View layout= inf.inflate(R.layout.toast_func_right, null);
				mlistview=(ListView)layout.findViewById(R.id.toast_func_list);
				this.addView(layout);
			}
		}else{
			LayoutInflater inf= LayoutInflater.from(context);
			View layout= inf.inflate(R.layout.toast_func, null);
			mlistview=(ListView)layout.findViewById(R.id.toast_func_list);
			this.addView(layout);
		}
		
	}
	public void setOnItemClickListener (AdapterView.OnItemClickListener listener){
		mlistview.setOnItemClickListener(listener);
	}
	public void setListData(String[] list){
		List<HashMap<String,String>> inlist=new ArrayList<HashMap<String,String>>();
		for(String text:list){
			HashMap<String,String> item=new HashMap<String,String>();
			item.put("text", text);
			inlist.add(item);
		}
		SimpleAdapter adapter=null;
		if(mpos.equals("middle")){
			adapter=new SimpleAdapter(getContext(), inlist, R.layout.simple_list_item_1_whilte, new String[]{"text"}, new int[]{android.R.id.text1});
		}else if(mpos.equals("right")){
			adapter=new SimpleAdapter(getContext(), inlist, R.layout.simple_list_item_1_black, new String[]{"text"}, new int[]{android.R.id.text1});
		}
		mlistview.setAdapter(adapter);
	}
	public void processHit(MotionEvent event){
		Rect rect=new Rect();
		
		getHitRect(rect);
		if((event.getX()<rect.left || event.getX()>rect.right) || (event.getY()<rect.top || event.getY()>rect.bottom)){
			hideanimation();
		}
	}
	public void showanimation(){
		setVisibility(View.VISIBLE);
		if(animationout==null){
			
			animationout = AnimationUtils.loadAnimation(getContext(), R.anim.toast_func_out);
		}
		this.startAnimation(animationout);
	}
	public void hideanimation(){
		if(animationin==null){
			
			animationin = AnimationUtils.loadAnimation(getContext(), R.anim.toast_func_in);
		}
		if(getVisibility()!=View.GONE){
			
			this.startAnimation(animationin);
			setVisibility(View.GONE);
		}
	}
}
